package com.example.administrator.androidapp;

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Ch6Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ch6_1);

        //在JAVA中访问字符串资源
        Log.d(Ch6Activity1.class.toString(),getString(R.string.ok));

        //访问数组资源
        Resources resources=getResources();
        String[] strArr=resources.getStringArray(R.array.strArr1);//访问字符串数组
        for(int i=0;i<strArr.length;i++){
            Log.i(Ch6Activity1.class.toString(),strArr[i]);
        }

        int[] intArr=resources.getIntArray(R.array.intArr);//访问整型数组
        for(int i=0;i<intArr.length;i++){
            Log.i(Ch6Activity1.class.toString(),intArr[i]+"");
        }

        TypedArray typedArray=resources.obtainTypedArray(R.array.commonArray);//访问TypedArray
        String string=typedArray.getString(0);
        Log.i(Ch6Activity1.class.toString(),string);
        //将TypedArray中引用的图像，显示到界面上。
        int resId=typedArray.getResourceId(2,0);//得到资源ID，第1个参数是索引，第2个是默认值
        ImageView imageView=(ImageView)findViewById(R.id.ch6_1_iv);
        imageView.setImageResource(resId);
        //将TypedArray中引用的数组，进行遍历。
        int arrayId=typedArray.getResourceId(3,0);
        strArr=resources.getStringArray(arrayId);//访问字符串数组
        for(int i=0;i<strArr.length;i++){
            Log.i(Ch6Activity1.class.toString(),strArr[i]+"----------from typedArray-----");
        }

        //在Activity的onCreate()方法中注册上下文菜单
        LinearLayout linearLayout=(LinearLayout) findViewById(R.id.ch6_1_ll);
        registerForContextMenu(linearLayout);
    }

    //重写Activity中的onCreateContextMenu()方法,将定义的菜单资源文件，加载到Menu对象中
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.mymenu,menu);
    }
    //重写onContextItemSelected ()方法，用于当菜单项被选择时，作出相应的处理。
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int id=item.getItemId();
        switch (id){
            case R.id.mymenu_item1:
                Toast.makeText(this,"mymenu_item1",Toast.LENGTH_LONG).show();
                break;
            case R.id.mymenu_item2:
                Toast.makeText(this,"mymenu_item2",Toast.LENGTH_LONG).show();
                break;
            case R.id.mymenu_item3:
                Toast.makeText(this,"mymenu_item3",Toast.LENGTH_LONG).show();
                break;
        }
        return true;
    }

    //重写Activity中的onCreateOptionsMenu()方法,将定义的菜单资源文件，加载到Menu对象中
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.mymenu,menu);
        return true;
    }
    //重写onOptionsItemSelected()方法，用于当菜单项被选择时，作出相应的处理
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id=item.getItemId();
        switch (id){
            case R.id.mymenu_item1:
                Toast.makeText(this,"mymenu_item1",Toast.LENGTH_LONG).show();
                break;
            case R.id.mymenu_item2:
                Toast.makeText(this,"mymenu_item2",Toast.LENGTH_LONG).show();
                break;
            case R.id.mymenu_item3:
                Toast.makeText(this,"mymenu_item3",Toast.LENGTH_LONG).show();
                break;
        }
        return true;
    }
}
